
import { useUserStore } from "@/store/user";
import { useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { generalRequestGet, generalRequestPost, getoptions, shengCode } from '@/api/home.js'

export const useTable = (api) => {
  let allOptionLsit = ref({});
  const state = reactive({
    searchParam: {},
    pagination: {
      pageSize: 10,
      pageNum: 1,
    },
    cacheParam: {},
    tableData: [],
    loading: false,
    total: 1,
    current: 1,
    pages: 1,
    tableRef: null,
    addOrupdate: false,
    dialogVisible: false,
    disabled: false,
    rowData: {},
    areaCode: [],
    tableRef: null,
    formInline: {},
    ruleFormRef: null,
    areaOption: []
  });
  const userStore = useUserStore();
  const router = useRouter();
  const route = useRoute();

  const getTable = () => {
    let params = Object.assign({}, state.searchParam, state.pagination);
    generalRequestPost(api, params).then((res) => {
      state.tableData = res.data.records ? res.data.records : res.data;
      state.tableData.map((d, index) => {
        d.index = index + 1 + 10 * (state.pagination.pageNum - 1)
      })
      state.total = res.data.total;
      state.pages = res.data.pages;
    });
  };
  watch(
    () => state.current,
    (newval, oldval) => {
      state.pagination.pageNum = newval;
      getTable();
    }
  );

  //子组件关闭dialog
  const closeDialog = () => {
    state.dialogVisible = false;
    getTable()
  };
  //新增
  const addRow = () => {
    state.dialogVisible = true;
    state.rowData = {};
    state.addOrupdate = true;
    state.disabled = false;
  };
  const updateRow = (row) => {
    state.rowData = row;
    state.addOrupdate = false;
    state.dialogVisible = true;
  };
  const deleteRowList = (api) => {
    let list = state.tableRef.getSelectionRows()
    if (list.length > 0) {
      ElMessageBox.confirm("确认删除选中的数据？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let arr = []
        list.map(d => {
          arr.push(d.id)
        })
        let params = {
          ids: list,
        };
        generalRequestPost(api, params).then((res) => {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          getTable();
        });
      });
    } else {
      ElMessage({
        message: "请先勾选需要删除的数据",
        type: "info",
      });
    }
  }
  const deleteRow = (row, api) => {
    ElMessageBox.confirm("确认删除此条数据？", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      let params = {
        ids: [row.id],
      };
      generalRequestPost(api, params).then((res) => {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        getTable();
      });
    });
  };
  const cityChange = (data) => {
    state.cityCode = "";
    state.countyCode = "";
    state.townCode = "";
    if (data[0] != 330000) {
      state.cityCode = data[0];
      state.countyCode = data[1] ? data[1] : "";
      state.townCode = data[2] ? data[2] : "";
    }
  }

  const reset = () => {
    state.searchParam = {};
    state.cacheParam = {};
    nextTick(() => {
      getTable();
    });
  };

  const getareaOption = () => {
    state.areaOption = []
    let params = {
      code: "330000",
    };
    shengCode(params).then(res => {
      res.data.forEach((d) => {
        let obj = {
          label: d.name,
          value: d.code,
        };
        state.areaOption.push(obj)
      })
      let newobj = { label: '浙江省', value: 330000 }
      state.areaOption.unshift(newobj)
    })
  }
  //根据传入的type，获取枚举
  const getAllOptionList = (type, objName, showName, parma) => {
    let parmas = {
      dicType: type,
    };
    parmas = Object.assign({}, parmas, parma);
    getoptions(parmas).then(res => {
      res.data.map((d) => {
        d.label = d[showName] || d.name;
        d.value = d.id + "";
      });
      objName ? allOptionLsit.value[objName] = res.data : allOptionLsit.value[type] = res.data;
    })
  }


  return {
    allOptionLsit,
    ...toRefs(state),
    getTable,
    reset,
    closeDialog,
    updateRow,
    addRow,
    deleteRow,
    getAllOptionList,
    cityChange,
    deleteRowList,
    getareaOption
  };
};
